﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;
public partial class ViewBookie : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Admin admin;
    List<Admin> admins;    
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        if (cookie == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.SuperAdmin))
            {
                if (!Page.IsPostBack)
                {
                    //if (Session["AdminUser"] != null)
                    //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];

                    bindAdmin();
                }
            }
            else
            {
                Response.Redirect("Logout.aspx");
            }
        }
    }

    private void bindAdmin()
    {
        admin = new Admin(_dataObject);
        admins = new List<Admin>();
        admins = admin.GetAllAdmin();
        if (admins.Count > 0)
        {
            rptAdmin.DataSource = admins;
            rptAdmin.DataBind();
        }
    }
    protected void drpBookie_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpBookie.SelectedValue == "1")
        {
            admin = new Admin(_dataObject);
            admins = new List<Admin>();
            admins = admin.GetAllAdmin();
            if (admins.Count > 0)
            {
                rptAdmin.DataSource = admins;
                rptAdmin.DataBind();
            }
        }
        else
        {
            admin = new Admin(_dataObject);
            admins = new List<Admin>();
            admins = admin.GetInActiveAdmin();
            if (admins.Count > 0)
            {
                rptAdmin.DataSource = admins;
                rptAdmin.DataBind();
            }
        }
    }
    protected void rptAdmin_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower().Trim() == "editadmin")
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deleteadmin"))
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnAdminId = (System.Web.UI.WebControls.Label)row.FindControl("hdnAdminId");
            admin = new Admin(_dataObject);
            AdminCredential adminCredentail = new AdminCredential(_dataObject);
            admin.Id = Convert.ToInt32(hdnAdminId.Text);
            admin.DeleteAdmin();
            divRed.InnerHtml = "<button data-dismiss='alert' class='close'></button><strong>Success! </strong>&nbsp;Record deleted successfully!";
            divRed.Attributes.Add("class", "alert alert-success");
            bindAdmin();
        }
    }
    protected void rptAdmin_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            System.Web.UI.WebControls.Label hdnAdminId = (System.Web.UI.WebControls.Label)e.Item.FindControl("hdnAdminId");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Item.FindControl("btnEdit");
            btnEdit.PostBackUrl = "BookieRegistration.aspx?Id=" + hdnAdminId.Text;
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("BookieRegistration.aspx");
    }
}